package com.qhs.framework.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class CSVUtils {
	 /**
     * 导出
     * @param dataList 数据
     */
    public  boolean exportCsv(File file, List<?> dataList,FileOutputStream out,OutputStreamWriter osw,BufferedWriter bw,
    	List<String> headers){
        boolean isSucess=false;
        int size = headers.size() - 1;

        try {
        	for (Object object : dataList) {
        		Class className = object.getClass();
        		for (int i = 0; i <= size - 1; i++) {
        			String fileName = headers.get(i);
        			String getMethodName = "get"+ fileName.substring(0, 1).toUpperCase()+ fileName.substring(1);
            		Method getMethod = className.getMethod(getMethodName, new Class[]{});
	                Object dataSingle = getMethod.invoke(object, new Object[]{});
	                writeCsv(dataSingle, bw);
				}
        		String fieldNameLast = headers.get(size);
        		String getMethodName = "get"+ fieldNameLast.substring(0, 1).toUpperCase()+ fieldNameLast.substring(1);
        		Method getMethod = className.getMethod(getMethodName, new Class[]{});
                Object dataSingle = getMethod.invoke(object, new Object[]{});
                writeCsvEnd(dataSingle, bw);
        	}
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                	bw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                	osw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                	out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }


    /**
     * 导出
     */
    public  boolean exportCsvOne(Object T,OutputStreamWriter osw,BufferedWriter bw,
    	List<String> headers){
        boolean isSucess=false;
        int size = headers.size() - 1;
        try {
    		Class className = T.getClass();
    		for (int i = 0; i <= size - 1; i++) {
    			String fileName = headers.get(i);
    			String getMethodName = "get"+ fileName.substring(0, 1).toUpperCase()+ fileName.substring(1);
        		Method getMethod = className.getMethod(getMethodName, new Class[]{});
                Object dataSingle = getMethod.invoke(T, new Object[]{});
                writeCsv(dataSingle, bw);
			}
    		String fieldNameLast = headers.get(size);
    		String getMethodName = "get"+ fieldNameLast.substring(0, 1).toUpperCase()+ fieldNameLast.substring(1);
    		Method getMethod = className.getMethod(getMethodName, new Class[]{});
            Object dataSingle = getMethod.invoke(T, new Object[]{});
            writeCsvEnd(dataSingle, bw);
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                	bw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                	osw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }


    private  void writeCsv(Object dataSingle,BufferedWriter bw) throws Exception{
    	 if (dataSingle == null) {
         	bw.append("\"").append(" ").append("\"").append(",");
			}
          if(dataSingle instanceof Number){
         	 NumberFormat format = NumberFormat.getInstance();
         	 String dataValue = format.format(dataSingle);
         	 bw.append("\"").append(dataValue).append("\"").append(",");
         }
         if(dataSingle instanceof Date){
         	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         	String dataValue = df.format(dataSingle);
         	bw.append("\"").append(dataValue).append("\"").append(",");
         }else if(dataSingle instanceof String){
         	bw.append("\"").append((String)dataSingle).append("\"").append(",");
         }
    }

    private  void writeCsvEnd(Object dataSingle,BufferedWriter bw) throws Exception{
   	 if (dataSingle == null) {
        	bw.append("\"").append(" ").append("\"").append("\r");
			}
         if(dataSingle instanceof Number){
        	 NumberFormat format = NumberFormat.getInstance();
        	 String dataValue = format.format(dataSingle);
        	 bw.append("\"").append(dataValue).append("\"").append("\r");
        }
        if(dataSingle instanceof Date){
        	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	String dataValue = df.format(dataSingle);
        	bw.append("\"").append(dataValue).append("\"").append("\r");
        }else if(dataSingle instanceof String){
        	bw.append("\"").append((String)dataSingle).append("\"").append("\r");
        }
   }

}
